<template>
  <div style="width:100%;height:auto; margin:0 auto;">
    <el-form ref="ruleForm" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="上级节点">
        <el-select v-model="form.parentId" placeholder="请选择" style="width:100%;">
          <el-option v-for="item in ggTypes" :key="item.id" :label="item.menuName" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="名称" prop="menuName">
        <el-input v-model="form.menuName" placeholder="名称" />
      </el-form-item>
      <el-form-item label="路径" prop="link">
        <el-input v-model="form.link" placeholder="路径" />
      </el-form-item>
      <el-form-item label="排序号" prop="sortNum">
        <el-input v-model="form.sortNum" placeholder="排序号" />
      </el-form-item>
    </el-form>
    <div style="text-align:center; padding-top:10px">
      <el-button type="primary" @click="edit('ruleForm')">保存</el-button>
      <el-button type="danger" @click="closePage">取消</el-button>
    </div>
  </div>
</template>

<script>
import { getInfo, edit, getAllLeafNode } from '@/api/configMenu'
import { Message } from 'element-ui'

export default {
  inject: ['getTreeList'],
  props: { // 第二种方式
    proid: {
      type: String,
      required: true
    },
    level: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      form: {},
      ggTypes: [],
      rules: {
        menuName: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getDetailed()
  },
  methods: {
    getDetailed() {
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })

      getInfo({ code: this.proid }).then(response => {
        this.form = response.data.configMenu
        console.log(this.form,'this.form')
        getAllLeafNode({ xiangmuId: response.data.configMenu.projectId }).then(response => {
          this.ggTypes = response.data
        }).catch(function() {
        })

        loading.close()
      }).catch(function() {
        loading.close()
      })
    },
    edit(ruleForm) { // 编辑
      this.$refs['ruleForm'].validate(valid => {
        if (valid) {
          const loading = this.$loading({
            lock: true,
            text: 'Loading',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          })
          this.form.level  = this.level
          edit(this.form).then(response => {
            Message({
              message: '编辑成功',
              type: 'success',
              duration: 5 * 1000
            })

            this.$emit('update:visible', false)
            loading.close()
            this.getTreeList()
          }).catch(response => {
            loading.close()
            this.getList()
          })
        } else {
          return false
        }
      })
    },
    closePage() {
      this.$emit('update:visible', false)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
